/**
* @version 1.0
* @package FACTURAPYMES.
* @author Jorge Washington Mueses Cevallos.
* @copyright Copyright (C) 2010 por Jorge Mueses. Todos los derechos reservados.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL.
* FACTURAPYMES! es un software de libre distribución, que puede ser
* copiado y distribuido bajo los términos de la Licencia Pública
* General GNU, de acuerdo con la publicada por la Free Software
* Foundation, versión 2 de la licencia o cualquier versión posterior.
*/

package jm.act;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;
import jm.act.clas.Activo;
import java.sql.ResultSet;

/**
 *
 * @author Jorge
 */
public class getSecCodigo extends HttpServlet {
    private String _ip = null;
    private int _puerto = 5432;
    private String _db = null;

    public void init(ServletConfig config) throws ServletException
    {
        this._ip = config.getServletContext().getInitParameter("_IP");
        this._puerto = Integer.parseInt(config.getServletContext().getInitParameter("_PUERTO"));
        this._db = config.getServletContext().getInitParameter("_DB");
    }
    /**
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        HttpSession sesion = request.getSession(true);
        String usuario = (String)sesion.getAttribute("usuario");
        String clave = (String)sesion.getAttribute("clave");
        
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "Mon, 01 Jan 2001 00:00:01 GMT");
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Cache-Control", "must-revalidate");
        response.setHeader("Cache-Control", "no-cache");

        PrintWriter out = response.getWriter();

        String codAct = request.getParameter("codAct");
        String obj = request.getParameter("obj");
        
        Activo objActivo = new Activo(this._ip, this._puerto, this._db, usuario, clave);
        try {
            String combo = "";
            
            try{
                String codigo = "";
                int secuencial = 0;
                ResultSet rs = objActivo.consulta("select max( replace(upper(codigo_activo), '"+codAct.toUpperCase()+"', '')::int8 ) from tbl_activo "
                    + "where upper(codigo_activo) like '"+codAct.toUpperCase()+"%';");
                if(rs != null){
                    while(rs.next()){
                        codigo = rs.getString(1)!=null ? rs.getString(1) : "";
                        secuencial = Integer.parseInt( codigo ) + 1;
                        combo += "<div onmouseover=\"this.className='jm_filaSobre\';this.style.cursor='pointer'\" onmouseout=\"this.className=''\" onclick=\"_('"+obj+"').value=this.innerHTML;_R('dv');\">"+ (codAct + secuencial) +"</div>";
                    }
                    rs.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            
            if(combo.compareTo("")!=0){
                out.print("obj»dv^frm»"+combo);
            }else{
                out.print("fun»_R('dv')");
            }
            
        } finally {
            objActivo.cerrar();
            out.close();
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
